<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="创建人" prop="createdBy">
      <el-input v-model="dataForm.createdBy" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createdTime">
      <el-input v-model="dataForm.createdTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新人" prop="updatedBy">
      <el-input v-model="dataForm.updatedBy" placeholder="更新人"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updatedTime">
      <el-input v-model="dataForm.updatedTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="是否删除" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="是否删除"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remarks">
      <el-input v-model="dataForm.remarks" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="是否显示时间 0：否 1:是" prop="type">
      <el-input v-model="dataForm.type" placeholder="是否显示时间 0：否 1:是"></el-input>
    </el-form-item>
    <el-form-item label="消息内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="消息内容"></el-input>
    </el-form-item>
    <el-form-item label="内容类型 0文字1图片2视频 3:礼物" prop="contentType">
      <el-input v-model="dataForm.contentType" placeholder="内容类型 0文字1图片2视频 3:礼物"></el-input>
    </el-form-item>
    <el-form-item label="是否已读" prop="isRead">
      <el-input v-model="dataForm.isRead" placeholder="是否已读"></el-input>
    </el-form-item>
    <el-form-item label="发送人" prop="sender">
      <el-input v-model="dataForm.sender" placeholder="发送人"></el-input>
    </el-form-item>
    <el-form-item label="接收人" prop="receiver">
      <el-input v-model="dataForm.receiver" placeholder="接收人"></el-input>
    </el-form-item>
    <el-form-item label="" prop="requestId">
      <el-input v-model="dataForm.requestId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="是否是最后一条消息 0:否 1:是" prop="isLast">
      <el-input v-model="dataForm.isLast" placeholder="是否是最后一条消息 0:否 1:是"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          createdBy: '',
          createdTime: '',
          updatedBy: '',
          updatedTime: '',
          delFlag: '',
          status: '',
          remarks: '',
          type: '',
          content: '',
          contentType: '',
          isRead: '',
          sender: '',
          receiver: '',
          requestId: '',
          isLast: ''
        },
        dataRule: {
          createdBy: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createdTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updatedBy: [
            { required: true, message: '更新人不能为空', trigger: 'blur' }
          ],
          updatedTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          remarks: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '是否显示时间 0：否 1:是不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '消息内容不能为空', trigger: 'blur' }
          ],
          contentType: [
            { required: true, message: '内容类型 0文字1图片2视频 3:礼物不能为空', trigger: 'blur' }
          ],
          isRead: [
            { required: true, message: '是否已读不能为空', trigger: 'blur' }
          ],
          sender: [
            { required: true, message: '发送人不能为空', trigger: 'blur' }
          ],
          receiver: [
            { required: true, message: '接收人不能为空', trigger: 'blur' }
          ],
          requestId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          isLast: [
            { required: true, message: '是否是最后一条消息 0:否 1:是不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/bbm/message/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createdBy = data.message.createdBy
                this.dataForm.createdTime = data.message.createdTime
                this.dataForm.updatedBy = data.message.updatedBy
                this.dataForm.updatedTime = data.message.updatedTime
                this.dataForm.delFlag = data.message.delFlag
                this.dataForm.status = data.message.status
                this.dataForm.remarks = data.message.remarks
                this.dataForm.type = data.message.type
                this.dataForm.content = data.message.content
                this.dataForm.contentType = data.message.contentType
                this.dataForm.isRead = data.message.isRead
                this.dataForm.sender = data.message.sender
                this.dataForm.receiver = data.message.receiver
                this.dataForm.requestId = data.message.requestId
                this.dataForm.isLast = data.message.isLast
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bbm/message/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'createdBy': this.dataForm.createdBy,
                'createdTime': this.dataForm.createdTime,
                'updatedBy': this.dataForm.updatedBy,
                'updatedTime': this.dataForm.updatedTime,
                'delFlag': this.dataForm.delFlag,
                'status': this.dataForm.status,
                'remarks': this.dataForm.remarks,
                'type': this.dataForm.type,
                'content': this.dataForm.content,
                'contentType': this.dataForm.contentType,
                'isRead': this.dataForm.isRead,
                'sender': this.dataForm.sender,
                'receiver': this.dataForm.receiver,
                'requestId': this.dataForm.requestId,
                'isLast': this.dataForm.isLast
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
